package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Admin;
import com.example.demo.dao.AdminDao;
import com.example.demo.entity.AdminQuery;
import com.example.demo.service.AdminService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Admin)表服务实现类
 *
 * @author makejava
 * @since 2020-05-26 10:59:39
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminDao adminDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Admin queryById(Integer id) {

        return this.adminDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public IPage<Admin> queryAllByLimit(int offset, int limit, AdminQuery bean) {
        Page<Admin> page = new Page<>(offset, limit);
//        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
//        wrapper.like("account",bean.getAccount())
//                .eq("mobile",bean.getMobile()).
//                gt("reg_date",bean.getStartTime()).
//                lt("reg_date",bean.getEndTime());
        page.setRecords(adminDao.queryAll(page, bean));
        return page;
    }

    @Override
    public List<Admin> queryAll(AdminQuery bean) {

        return null;
    }

    /**
     * 新增数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public Admin insert(Admin admin) {
        this.adminDao.insert(admin);
        return admin;
    }

    /**
     * 修改数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Admin admin) {

        return this.adminDao.update(admin);
    }


    @Override
    public boolean deleteById(List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return false;
        }
        StringBuffer sb = new StringBuffer("id in (");
        for (Integer item : ids) {
            sb.append("'");
            sb.append(item);
            sb.append("',");
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append(")");
        return this.adminDao.delete("stu.admin", sb.toString()) > 0;
    }

    @Override
    public boolean deleteById1(Integer id) {
        if (id == null) {
            return false;
        }
        StringBuffer sb = new StringBuffer("id =");
        sb.append(id);
        return this.adminDao.delete1("stu.admin", sb.toString()) > 0;
    }


    @Override
    public Admin login(String account, String password) {
        return this.adminDao.login(account, password);
    }
}